package groupb6.shopsystem.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import groupb6.shopsystem.mapper.UserMapper;
import groupb6.shopsystem.pojo.entity.User;
import groupb6.shopsystem.util.response.ResponseException;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.websocket.Session;
import java.util.List;
import java.util.Map;

@Component
public class JWTUtil {
    @Resource
    private UserMapper userMapper;

    private static UserMapper staticUserMapper;

    public static String secure = "shopsystem密钥";

    @PostConstruct
    private void initStaticUserDAO() {
        staticUserMapper = userMapper;
    }

    public static String createToken(String account) {
        return JWT.create().withAudience(account)
                .sign(Algorithm.HMAC256(JWTUtil.secure));
    }

    public static User getCurrentUser() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if (Strings.isNotBlank(token)) {
            return staticUserMapper.getUserByAccount(JWT.decode(token).getAudience().get(0));
        }
        throw new ResponseException(401, "未持有token");
    }
    /*
    * 对于Websocket连接获取token的方式
    * */
    public static User getCurrentUserWS(Session session) {
        Map<String, List<String>> queryParams = session.getRequestParameterMap();
        String token=queryParams.get("token").get(0);
        if (Strings.isNotBlank(token)) {
            return staticUserMapper.getUserByAccount(JWT.decode(token).getAudience().get(0));
        }
        throw new RuntimeException();
    }
}
